package com.liy.cloud.common.trade.entity;

import com.liy.cloud.common.exception.NegativeNumberException;
import com.liy.cloud.common.trade.utils.TradeMessageParser;

import java.io.Serializable;
import java.text.ParseException;

/**
 * 联程出站交易数据明细实体类 作者: 日期：2018-08-12 17:12:33 描述：
 */
public class TradeExitTransfer extends TradeComm implements Serializable {

	private static final long serialVersionUID = 1L;

	/*
	 * 主表ID
	 */
	private String commId;

	/*
	 * 进站设备ID
	 */
	private String enterDeviceId;

	/*
	 * 进站站点
	 */
	private String enterAddress;

	/*
	 * 进站时间
	 */
	private String enterTime;

	/*
	 * 前次交易终端代号
	 */
	private String exDeviceId;

	/*
	 * 前次交易类型
	 */
	private String exTradeCode;

	/*
	 * 前次交易时间
	 */
	private String exTradeTime;

	/*
	 * 前次交易金额
	 */
	private Long exTradeMoney;

	/*
	 * 前次交易操作员号
	 */
	private String exOper;

	/*
	 * 卡当前核准状态
	 */
	private String approveStatus;

	/*
	 * 卡当前核准时间
	 */
	private String approveTime;

	/*
	 * 卡当前核准站点
	 */
	private String approveStation;

	/*
	 * 最近联程使用时间
	 */
	private String transferTime;

	/*
	 * 联程优惠金额
	 */
	private Long transferMoney;

	/*
	 * 换乘企业编号
	 */
	private String companyNo;

	/*
	 * 换乘线路编号
	 */
	private String lineNo;

	/*
	 * 换乘最近使用站点
	 */
	private String transferAddress;

	/**
	 * 默认构造方法
	 **/
	public TradeExitTransfer() {
		super();
	}

	/**
	 * getger和setter方法
	 */

	public String getCommId() {
		return commId;
	}

	public void setCommId(String commId) {
		this.commId = commId;
	}

	public String getEnterDeviceId() {
		return enterDeviceId;
	}

	public void setEnterDeviceId(String enterDeviceId) {
		this.enterDeviceId = enterDeviceId;
	}

	public String getEnterAddress() {
		return enterAddress;
	}

	public void setEnterAddress(String enterAddress) {
		this.enterAddress = enterAddress;
	}

	public String getEnterTime() {
		return enterTime;
	}

	public void setEnterTime(String enterTime) throws ParseException {
		checkTimeStyle(enterTime, "EnterTime");
		this.enterTime = enterTime;
	}

	public String getExDeviceId() {
		return exDeviceId;
	}

	public void setExDeviceId(String exDeviceId) {
		this.exDeviceId = exDeviceId;
	}

	public String getExTradeCode() {
		return exTradeCode;
	}

	public void setExTradeCode(String exTradeCode) {
		this.exTradeCode = exTradeCode;
	}

	public String getExTradeTime() {
		return exTradeTime;
	}

	public void setExTradeTime(String exTradeTime) throws ParseException {
		checkTimeStyle(exTradeTime, "ExTradeTime");
		this.exTradeTime = exTradeTime;
	}

	public Long getExTradeMoney() {
		return exTradeMoney;
	}

	public void setExTradeMoney(Long exTradeMoney) throws NegativeNumberException {
		checkMoneyStyle(exTradeMoney, "ExTradeMoney");
		this.exTradeMoney = exTradeMoney;
	}

	public String getExOper() {
		return exOper;
	}

	public void setExOper(String exOper) {
		this.exOper = exOper;
	}

	public String getApproveStatus() {
		return approveStatus;
	}

	public void setApproveStatus(String approveStatus) {
		this.approveStatus = approveStatus;
	}

	public String getApproveTime() {
		return approveTime;
	}

	public void setApproveTime(String approveTime) throws ParseException {
		checkTimeStyle(approveTime, "approveTime");
		this.approveTime = approveTime;
	}

	public String getApproveStation() {
		return approveStation;
	}

	public void setApproveStation(String approveStation) {
		this.approveStation = approveStation;
	}

	public String getTransferTime() {
		return transferTime;
	}

	public void setTransferTime(String transferTime) throws ParseException {
		checkTimeStyle(transferTime, "TransferTime");
		this.transferTime = transferTime;
	}

	public Long getTransferMoney() {
		return transferMoney;
	}

	public void setTransferMoney(Long transferMoney) throws NegativeNumberException {
		checkMoneyStyle(transferMoney, "TransferMoney");
		this.transferMoney = transferMoney;
	}

	public String getCompanyNo() {
		return companyNo;
	}

	public void setCompanyNo(String companyNo) {
		this.companyNo = companyNo;
	}

	public String getLineNo() {
		return lineNo;
	}

	public void setLineNo(String lineNo) {
		this.lineNo = lineNo;
	}

	public String getTransferAddress() {
		return transferAddress;
	}

	public void setTransferAddress(String transferAddress) {
		this.transferAddress = transferAddress;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 * 根据版本容解析交易内容
	 * 
	 * @param message
	 * @throws Exception
	 */
	public TradeExitTransfer init(String message) throws Exception {



		TradeMessageParser messageParser = null;
		if(message.startsWith("SZMETROV2")) {

			// 根据报文格式长度来定义， -1为行业分割符
			int[] splitNumber = { 9, 8, 9, 2, 10, 9, 6, 14, 10, 8, 8, 8, 5, 10, 14, 9, 10, 1, 14, 10, 9, 2, 14, 8, 6, 20,
					10, 2, 2, 16, 1, 1, 3, 4, 14, 10, 8, 2, 2, 2, 4, 4, 1, 4, 5, -1, 5 };
			messageParser = new TradeMessageParser(message, splitNumber);

			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setTradeTime(messageParser.getFiled(7));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(8)));
			this.setTradeMoney(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeValue(Long.parseLong(messageParser.getFiled(10)));
			this.setBalance(Long.parseLong(messageParser.getFiled(11))); // 余额
			this.setCardSeq(Long.parseLong(messageParser.getFiled(12)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(13)));
			this.setEnterTime(messageParser.getFiled(14));

			this.setEnterDeviceId(messageParser.getFiled(15));
			this.setEnterAddress(messageParser.getFiled(16));
			this.setApproveStatus(messageParser.getFiled(17));
			this.setApproveTime(messageParser.getFiled(18));
			this.setApproveStation(messageParser.getFiled(19));

			this.setExDeviceId(messageParser.getFiled(20));
			this.setExTradeCode(messageParser.getFiled(21));
			this.setExTradeTime(messageParser.getFiled(22));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(23)));
			this.setExOper(messageParser.getFiled(24));

			this.setSamcardId(messageParser.getFiled(25));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(26)));
			this.setCardCode(messageParser.getFiled(27));
			this.setTradeSub(messageParser.getFiled(28));
			this.setPhysicalId(messageParser.getFiled(29));

			this.setTestFlag(messageParser.getFiled(30));
			this.setGreyFlag(messageParser.getFiled(31));
			this.setCompanyNo(messageParser.getFiled(32));
			this.setLineNo(messageParser.getFiled(33));
			this.setTransferTime(messageParser.getFiled(34));

			this.setTransferAddress(messageParser.getFiled(35));
			this.setTransferMoney(Long.parseLong(messageParser.getFiled(36)));
			this.setMedia(messageParser.getFiled(37));
			this.setCardVersion(messageParser.getFiled(38));
			this.setSubCardCode(messageParser.getFiled(39));

			this.setCityCode(messageParser.getFiled(40));
			this.setBusinessCode(messageParser.getFiled(41));

			this.setIndustrySplit(messageParser.getFiled(42));
			this.setIssuer(messageParser.getFiled(43));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(44)));
			super.setIndustryData(messageParser.getFiled(45));
			super.setCrc16(messageParser.getFiled(46));

		} else if(message.startsWith("SZMETROV3")) {

			/** 根据报文格式长度来定义， -1为行业分割符
			 * 一、V3卡号由9位变为20位
			 * 二、增加字段：
			 付费方式
			 支付方式
			 发卡方机构码
			 发卡方交易流水号
			 运营模式
			 三、票卡发行方标识	从长度4变为长度2
			 */
			int[] splitNumber = {
					9, 8, 20, 2, 10,   9, 6, 14, 10, 8,   8, 8, 5, 10, 14,   9, 10, 1, 14, 10,   9, 2, 14, 8, 6,
					20,	10, 2, 2, 16,  1, 1, 3, 4, 14,   10, 8, 2, 2, 2,   4, 4,
					1, 2, 8, 10, 4,    //V3增加的字段
					1, 2, 5,
					-1, 5};
			messageParser = new TradeMessageParser(message, splitNumber);
			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setTradeTime(messageParser.getFiled(7));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(8)));
			this.setTradeMoney(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeValue(Long.parseLong(messageParser.getFiled(10)));
			this.setBalance(Long.parseLong(messageParser.getFiled(11))); // 余额
			this.setCardSeq(Long.parseLong(messageParser.getFiled(12)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(13)));
			this.setEnterTime(messageParser.getFiled(14));

			this.setEnterDeviceId(messageParser.getFiled(15));
			this.setEnterAddress(messageParser.getFiled(16));
			this.setApproveStatus(messageParser.getFiled(17));
			this.setApproveTime(messageParser.getFiled(18));
			this.setApproveStation(messageParser.getFiled(19));

			this.setExDeviceId(messageParser.getFiled(20));
			this.setExTradeCode(messageParser.getFiled(21));
			this.setExTradeTime(messageParser.getFiled(22));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(23)));
			this.setExOper(messageParser.getFiled(24));

			this.setSamcardId(messageParser.getFiled(25));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(26)));
			this.setCardCode(messageParser.getFiled(27));
			this.setTradeSub(messageParser.getFiled(28));
			this.setPhysicalId(messageParser.getFiled(29));

			this.setTestFlag(messageParser.getFiled(30));
			this.setGreyFlag(messageParser.getFiled(31));
			this.setCompanyNo(messageParser.getFiled(32));
			this.setLineNo(messageParser.getFiled(33));
			this.setTransferTime(messageParser.getFiled(34));

			this.setTransferAddress(messageParser.getFiled(35));
			this.setTransferMoney(Long.parseLong(messageParser.getFiled(36)));
			this.setMedia(messageParser.getFiled(37));
			this.setCardVersion(messageParser.getFiled(38));
			this.setSubCardCode(messageParser.getFiled(39));

			this.setCityCode(messageParser.getFiled(40));
			this.setBusinessCode(messageParser.getFiled(41));

			// V3增加字段
			this.setPayMethod(messageParser.getFiled(42));
			this.setPayType(messageParser.getFiled(43));
			this.setIssuerCode(messageParser.getFiled(44));
			this.setIssuerSeq(messageParser.getFiled(45));
			this.setOperMode(messageParser.getFiled(46));

			this.setIndustrySplit(messageParser.getFiled(47));
			this.setIssuer(messageParser.getFiled(48));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(49)));
			super.setIndustryData(messageParser.getFiled(50));
			super.setCrc16(messageParser.getFiled(51));

		}

		return this;
	}
}